package com.kaishengit.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.kaishengit.entity.Message;
import com.kaishengit.entity.User;
import com.kaishengit.util.DBHelp;
import com.kaishengit.util.RowMapper;

public class MessageDao {
	public DBHelp<Message> db=new DBHelp<Message>();
	
	public long findCount(String id) {
		String sql = "select count(*) from t_message where t_project_id = ?";
		return (Long) db.executeForFunction(sql, id);
	}

	public boolean saveInformation(String mid, String content, String date, String userid,
			String projectid,String type) {
		String sql="INSERT INTO t_message(id,content,`DATE`,t_user_id,t_project_id,type)VALUES(?,?,?,?,?,?)";
		return db.executeSQL(sql, mid,content,date,userid,projectid,type);
	}

	public boolean saveIdea(Message m) {
		String sql="INSERT INTO t_message(id,content,`DATE`,t_user_id,t_idea_id,`name`,t_project_id,`TYPE`)VALUES(?,?,?,?,?,?,?,?)";
		return db.executeSQL(sql,m.getId(),m.getContent(),m.getDate(),m.getUserid(),m.getIdeaid(),m.getName(),m.getProjectid(),m.getType());
	}

	public boolean saveGoal(Message mes) {
		String sql="INSERT INTO t_message(id,content,`DATE`,t_user_id,t_goal_id,`name`,t_project_id,`TYPE`)VALUES(?,?,?,?,?,?,?,?)";
		return db.executeSQL(sql,mes.getId(),mes.getContent(),mes.getDate(),mes.getUserid(),mes.getGoalid(),mes.getName(),mes.getProjectid(),mes.getType());
		
	}

	public boolean saveTask(Message m) {
		String sql="INSERT INTO t_message(id,content,`DATE`,t_user_id,t_task_id,`name`,t_project_id,`TYPE`)VALUES(?,?,?,?,?,?,?,?)";
		return db.executeSQL(sql,m.getId(),m.getContent(),m.getDate(),m.getUserid(),m.getTaskid(),m.getName(),m.getProjectid(),m.getType());
		
	}

	public boolean saveDocument(Message m) {
		String sql="INSERT INTO t_message(id,content,`DATE`,t_user_id,t_document_id,`name`,t_project_id,`TYPE`)VALUES(?,?,?,?,?,?,?,?)";
		return db.executeSQL(sql,m.getId(),m.getContent(),m.getDate(),m.getUserid(),m.getDocumentid(),m.getName(),m.getProjectid(),m.getType());
		
	}

	public boolean saveFile(Message m) {
		String sql="INSERT INTO t_message(id,`DATE`,t_user_id,t_file_id,`name`,t_project_id,`TYPE`)VALUES(?,?,?,?,?,?,?)";
		return db.executeSQL(sql,m.getId(),m.getDate(),m.getUserid(),m.getMyfileid(),m.getName(),m.getProjectid(),m.getType());
		
	}
	public List<Message> findAllByProjectId(String projectid,int offest, int pageSize){
		StringBuilder sql=new StringBuilder();
		sql.append("SELECT tm.id,tm.content,tm.date,tm.t_user_id,t_task_id,t_document_id,t_file_id,t_idea_id,t_goal_id,tm.name,t_project_id,tm.type,tu.username,tu.password,tu.imgname FROM t_message AS tm ");
		sql.append("LEFT JOIN t_user AS tu ON tm.t_user_id=tu.id ");
		sql.append("WHERE t_project_id=? ORDER BY tm.date DESC LIMIT ?,?");
		return db.executeQueryForList(sql.toString(), new MessageRowMapper(),projectid,offest,pageSize);
	}
	
	public class MessageRowMapper implements RowMapper<Message>{

		public Message mapRow(ResultSet rs) throws SQLException {
			Message me=new Message();
			me.setId(rs.getString("id"));
			me.setContent(rs.getString("content"));
			me.setDate(rs.getString("date"));
			me.setUserid(rs.getString("t_user_id"));
			me.setTaskid(rs.getString("t_task_id"));
			me.setDocumentid(rs.getString("t_document_id"));
			me.setGoalid(rs.getString("t_goal_id"));
			me.setMyfileid(rs.getString("t_file_id"));
			me.setIdeaid(rs.getString("t_user_id"));
			me.setProjectid(rs.getString("t_project_id"));
			me.setName(rs.getString("name"));
			me.setType(rs.getString("type"));
			
			User user=new User();
			user.setId(rs.getString("t_user_id"));
			user.setUsername(rs.getString("username"));
			user.setPassword(rs.getString("password"));
			user.setImgname(rs.getString("imgname"));
			me.setUser(user);
			
			return me;
		}
		
	}

	public Message findByDocumentId(String documentid) {
		String sql="SELECT id,content,`DATE`,t_user_id,t_task_id,t_document_id,t_file_id,t_idea_id,t_goal_id,`name`,t_project_id,`type` FROM t_message WHERE t_document_id=?";
		return db.executeQueryForObject(sql, new MRowMapper(), documentid);
	}
	
	public class MRowMapper implements RowMapper<Message>{

		public Message mapRow(ResultSet rs) throws SQLException {
			Message me=new Message();
			me.setId(rs.getString("id"));
			me.setContent(rs.getString("content"));
			me.setDate(rs.getString("date"));
			me.setUserid(rs.getString("t_user_id"));
			me.setTaskid(rs.getString("t_task_id"));
			me.setDocumentid(rs.getString("t_document_id"));
			me.setGoalid(rs.getString("t_goal_id"));
			me.setMyfileid(rs.getString("t_file_id"));
			me.setIdeaid(rs.getString("t_user_id"));
			me.setProjectid(rs.getString("t_project_id"));
			me.setName(rs.getString("name"));
			me.setType(rs.getString("type"));
			return me;
		}
		
	}

	public Message findByIdeaId(String ideaId) {
		String sql="SELECT id,content,`DATE`,t_user_id,t_task_id,t_document_id,t_file_id,t_idea_id,t_goal_id,`name`,t_project_id,`type` FROM t_message WHERE t_idea_id=?";
		return db.executeQueryForObject(sql, new MRowMapper(), ideaId);
	}

	public void delByDocumentId(String id) {
		String sql = "delete t_message where t_document_id = ?";
		db.executeSQL(sql, id);
		
	}

	public void delByFileId(String id) {
		String sql = "delete from t_message where t_file_id = ?";
		db.executeSQL(sql, id);
		
	}
	
	public boolean del(String id){
		String sql="delete from t_message where t_idea_id=?";
		return db.executeSQL(sql, id);
	}
	

}
